﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace destructor_demo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            SampleClass.OnCountChanged += new Action<int>(SampleClass_OnCountChanged);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            GC.Collect();
        }

        private void btnAllocate_Click(object sender, EventArgs e)
        {            
            for (int i = 0; i < int.Parse(tbToAllocate.Text); i++)
                new SampleClass();
        }

        public void SampleClass_OnCountChanged(int Count)
        {
            ChangeLabelText("Number of objects: " + Count.ToString());
        }

        private void ChangeLabelText(string Text)
        {
            if (lblCount.InvokeRequired)
                lblCount.Invoke(new Action<string>(ChangeLabelText), new object[] { Text });
            else
                lblCount.Text = Text;
        }
    }
}
